#include <stdlib.h>
#include <time.h>

#include "topology.h"

struct topology* new_cluster()
{
    int i;
    struct topology* result;
    
    result = (struct topology*)malloc(sizeof(struct topology));
    for(i = 0; i < 4; i++) {
        result->coridor[i] = 0;
        result->segments[i] = NULL;
    }
    result->depth = 0;
    
    return result;
}

void destroy_cluster(struct topology * to_destroy)
{
    int i;
    if(to_destroy->segments[0])
        for(i = 0; i < 4; i++)
            destroy_cluster(to_destroy->segments[i]);
    free(to_destroy);
}

void set_topology(struct topology* cluster)
{
    int type, i;
    
    type = rand()%5;
    
    for(i = 0; i< 4; i++)
        cluster->coridor[i] = 1;
    
    if(type < 4)
        cluster->coridor[type] = 0;
}

struct topology* generate_topology(int depth)
{
    struct topology *result;
    int i;
    
    
    result = new_cluster();
    set_topology(result);
    result->depth = depth;
    if(depth > 0)
        for(i = 0; i < 4; i++)
            result->segments[i] = generate_topology(depth - 1);
    return result;
}
